package com.hmtech.aup.presentation.aiedit

import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.doOnLayout
import androidx.core.view.updatePadding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.hmtech.aup.R
import com.hmtech.aup.util.GlideEngine
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.style.PictureSelectorStyle
import com.luck.picture.lib.style.TitleBarStyle


class AiEditActivity : AppCompatActivity() {
    private lateinit var sheetBehavior: BottomSheetBehavior<FrameLayout>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ai_edit)

        // 设置 Toolbar 的顶部 Padding（兼容状态栏高度）
        val toolbar = findViewById<Toolbar>(R.id.toolbar)

        val sheet = findViewById<FrameLayout>(R.id.media_sheet)
        sheetBehavior = BottomSheetBehavior.from(sheet)

        toolbar.post {
            val statusBarHeight = getStatusBarHeight(this)

            val lp = toolbar.layoutParams as CoordinatorLayout.LayoutParams
            lp.topMargin = statusBarHeight
            toolbar.layoutParams = lp

            calculatePeekHeight(toolbar.height)

            openMediaSelector()
        }
    }

    fun openMediaSelector() {
        val style = PictureSelectorStyle()
        style.titleBarStyle = TitleBarStyle()
        style.titleBarStyle = TitleBarStyle()
        style.titleBarStyle.isHideCancelButton = true
        style.titleBarStyle.isDisplayTitleBarLine = false
        style.titleBarStyle.titleLeftBackResource =
            com.luck.picture.lib.R.color.ps_color_transparent

        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofImage())
            .setSelectorUIStyle(style)
            .setImageEngine(GlideEngine.createGlideEngine())
            .setSelectionMode(SelectModeConfig.SINGLE)
            .isPreviewImage(false)
            .isDirectReturnSingle(true)
            .buildLaunch(R.id.media_picker_container,
                object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                    }

                    override fun onCancel() {

                    }
                })
    }

    fun getStatusBarHeight(context: Context): Int {
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId > 0) context.resources.getDimensionPixelSize(resourceId) else 0
    }

    private fun calculatePeekHeight(toolbarHeight: Int) {
        // 获取屏幕高度（不含导航栏）
        val screenHeight = Resources.getSystem().displayMetrics.heightPixels

        // 获取状态栏高度
        val statusBarHeight = getStatusBarHeight(this)

        // 计算可用高度：屏幕高度 - 状态栏 - Toolbar
        val availableHeight = screenHeight - statusBarHeight - toolbarHeight

        // 设置 peekHeight 为可用高度的一半
        sheetBehavior.peekHeight = availableHeight / 2
        // 最大高度
        sheetBehavior.maxHeight = availableHeight

        // 初始状态设置为折叠
        sheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }
}